﻿/**
 * ...
 * @author alijaya
 */

package hevex;

class EventDispatcher 
{

	private var events:Hash<List<Dynamic>>;
	
	public function new() 
	{
		events = new Hash<List<Dynamic>>();
	}
	
	public function addEventListener(type:String, listener:Dynamic)
	{
		if (!events.exists(type)) events.set(type, new List<Dynamic>());
		var l:List<Dynamic> = events.get(type);
		l.add(listener);
	}
	
	public function removeEventListener(type:String, listener:Dynamic)
	{
		var l:List<Dynamic> = events.get(type);
		if (l != null)
		{
			l.remove(listener);
			if (l.length == 0) events.remove(type);
		}
	}
	
	private function dispatchEvent(event:Event)
	{
		event.target = this;
		var l:List<Dynamic> = events.get(event.type);
		if (l != null)
		{
			for (f in l)
			{
				f(event);
			}
		}
	}
}